﻿using GKQC.DbHelper.ViewModels;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.VisualStyles;

namespace GKQC.DbHelper.DbModels
{
    [SugarTable("t_fqc_head")]
    public class FqcHeadModel
    {
        public FqcHeadModel()
        {
            this.Datas = new List<FqcDataModel>();
            this.ElectricViewModels = new List<ElectricViewModel>();
            this.SizeViewModels = new List<SizeViewModel>();
            this.Standard = new StandardView();
            this.TestDate = DateTime.Now;
            this.CreateDate = DateTime.Now;
            this.SNo = 1;

            this.Pn = string.Empty;
            this.Customer = string.Empty;
            this.LotNo = string.Empty;
            this.SourceCode = string.Empty;
            this.SourceNo = string.Empty;
            this.Remark = string.Empty;
            this.Apperance = string.Empty;
            this.ApproveUser = string.Empty;
            this.Machine = string.Empty;
            this.ModiUser = string.Empty;
            this.Series = string.Empty;
            this.Size = string.Empty;
            this.Spec = string.Empty;
            this.UserCode = string.Empty;
            this.ZFreq = string.Empty;
            this.RFreq = string.Empty;
        }

        /// <summary>
		/// 流水号
        /// </summary>	
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public long Id { get; set; }
        /// <summary>
        /// 来源单别
        /// </summary>		
        public string SourceCode { get; set; }
        /// <summary>
        /// 来源单号
        /// </summary>		
        public string SourceNo { get; set; }
        /// <summary>
        /// 批号
        /// </summary>		
        public string LotNo { get; set; }
        /// <summary>
        /// 批号项次
        /// </summary>		
        public int SNo { get; set; }
        /// <summary>
        /// 检验日期
        /// </summary>		
        public DateTime TestDate { get; set; }
        /// <summary>
        /// 入库数量
        /// </summary>		
        public int Qty { get; set; }
        /// <summary>
        /// 料号
        /// </summary>		
        public string Pn { get; set; }
        /// <summary>
        /// 系列
        /// </summary>		
        public string Series { get; set; }
        /// <summary>
        /// 规格
        /// </summary>		
        public string Spec { get; set; }
        /// <summary>
        /// 尺寸
        /// </summary>		
        public string Size { get; set; }

        /// <summary>
        /// 外观
        /// </summary>		
        public string Apperance { get; set; }
        /// <summary>
        /// 机台
        /// </summary>		
        public string Machine { get; set; }
        /// <summary>
        /// 客户
        /// </summary>		
        public string Customer { get; set; }
        /// <summary>
        /// 电气检测数量
        /// </summary>		
        public int ElectricQty { get; set; }
        /// <summary>
        /// 尺寸检测数量
        /// </summary>		
        public int SizeQty { get; set; }
        /// <summary>
        /// 用户代号
        /// </summary>		
        public string UserCode { get; set; }
        /// <summary>
        /// 建立日期
        /// </summary>		
        public DateTime CreateDate { get; set; }
        /// <summary>
        /// 建立人员
        /// </summary>		
        public string CreateUser { get; set; }
        /// <summary>
        /// 更新日期
        /// </summary>		
        public DateTime? ModiDate { get; set; }
        /// <summary>
        /// 修改人
        /// </summary>		
        public string ModiUser { get; set; }
        /// <summary>
        /// 审核日期
        /// </summary>		
        public DateTime? ApproveDate { get; set; }
        /// <summary>
        /// 审核人
        /// </summary>		
        public string ApproveUser { get; set; }
        /// <summary>
        /// 备注
        /// </summary>		
        public string Remark { get; set; }
        /// <summary>
        /// Z值频率
        /// </summary>		
        public string ZFreq { get; set; }
        /// <summary>
        /// R值频率
        /// </summary>		
        public string RFreq { get; set; }

        public int ReportId { get; set; }

        /// <summary>
        /// 容量上限(%)
        /// </summary>		
        public double CapMax { get; set; }
        /// <summary>
        /// 容量下限(%)
        /// </summary>		
        public double CapMin { get; set; }
        /// <summary>
        /// DF上限(%)
        /// </summary>		
        public double DfMax { get; set; }
        /// <summary>
        /// LC上限(μA)
        /// </summary>		
        public double LcMax { get; set; }
        /// <summary>
        /// Z值上限(mΩ)
        /// </summary>		
        public double ZMax { get; set; }
        /// <summary>
        /// ESR上限(mΩ)
        /// </summary>		
        public double EsrMax { get; set; }
        /// <summary>
        /// D上限(mm)
        /// </summary>		
        public double DMax { get; set; }
        /// <summary>
        /// L上限(mm)
        /// </summary>		
        public double LMax { get; set; }

        /// <summary>
        /// B束腰上限(mm)
        /// </summary>		
        public double BMax { get; set; }

        [SugarColumn(IsIgnore = true)]
        public List<FqcDataModel> Datas { get; set; }

        [SugarColumn(IsIgnore = true)]
        public List<ElectricViewModel> ElectricViewModels { get; set; }
        [SugarColumn(IsIgnore = true)]
        public List<SizeViewModel> SizeViewModels { get; set; }
        [SugarColumn(IsIgnore = true)]
        public StandardView Standard { get; set; }

        public double GetVoltage()
        {
            double voltage = 0;
            if(!string.IsNullOrWhiteSpace(this.Spec))
            {
                string[] ss = Spec.Split('/');
                if(ss.Length == 2)
                {
                    double.TryParse(ss[1].Replace("V",string.Empty).Replace("v",string.Empty), out voltage);
                }
            }
            return voltage;
        }

        public double GetCap()
        {
            double cap = 0;
            if (!string.IsNullOrWhiteSpace(this.Spec))
            {
                string[] ss = Spec.Split('/');
                if (ss.Length == 2)
                {
                    string s = ss[0].Replace("u", string.Empty).Replace("F", string.Empty).Replace("f", string.Empty)
                        .Replace("p", string.Empty).Replace("n", string.Empty)
                        .Replace("m", string.Empty).Replace("μ", string.Empty);
                    double.TryParse(s, out cap);
                }
            }
            return cap;
        }

        public void GenerateList(int electricQty, int sizeQty)
        {
            ViewModelTrans<FqcDataModel>.GenerateTestList(electricQty, sizeQty, SNo, ElectricViewModels, SizeViewModels);
        }
    }
}
